package com.zoulj.web;

import cn.hutool.extra.mail.MailUtil;
import com.zoulj.pojo.R;
import com.zoulj.util.JwtUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(tags="测试")
@RestController
@RequestMapping("/zou/test")
public class TestController {

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Autowired
    JwtUtil jwtUtil;

    // 发送消息
    @GetMapping("/kafka/{message}")
    public void sendMessage1(@PathVariable("message") String normalMessage) {
        //kafkaTemplate.send("topicA", normalMessage);
        kafkaTemplate.send("test", normalMessage);
    }

    @GetMapping("token/get")
    public R getToken(Long baseInfoId) {
        String token = jwtUtil.createJWT("130217","root","admin");
        return new R(token);
    }

    @GetMapping("/send/email")
    public R sendEmail(){
        //hutool发邮件
        String text = MailUtil.send("464480515@qq.com", "title.test", "mail.send.test", false, null);
        System.out.println(text);
        return new R();
    }

    @GetMapping("/rabbit/{code}")
    public void test(@PathVariable("code") String code) {
        Map<String,String> map = new HashMap<>();
        map.put("mobile","15073311125");
        map.put("code",code);
        rabbitTemplate.convertAndSend("sms",map);
        // 发送消息 参数分别为：交换机名称、路由键、消息内容 配置更加灵活
        rabbitTemplate.convertAndSend("topicExchange",
                "topic.abc",//topic.*
                "苹果来了10斤");
    }
}
